package cn.shenchao.jms.publish;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
/**
 * 消息订阅者1
 * @author chao
 *
 */
public class JMSConsumer {
	public static void main(String[] args)throws Exception {
		// 链接工厂
		ConnectionFactory connectionFactory = null;
		// 连接
		Connection connection;
		// 会话接收或者发送消息的线程
		Session session;
		// 消息目的地
		Destination destination;
		connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,
				ActiveMQConnection.DEFAULT_PASSWORD, ActiveMQConnection.DEFAULT_BROKER_URL);
		connection = connectionFactory.createConnection();
		connection.start();
		session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
		//destination = session.createQueue("FirstQueue1");
		destination = session.createTopic("sub");
		MessageConsumer messageConsumer = session.createConsumer(destination);
		messageConsumer.setMessageListener(new MessageListener() {
			
			@Override
			public void onMessage(Message message) {
				TextMessage textMessage = (TextMessage) message;
				if (textMessage != null) {
					try {
						System.out.println("consumer1 收到了消息" + textMessage.getText());
					} catch (Exception e) {
						e.printStackTrace();
					}

				}				
			}
		});
	}
}
